package com.bootdo.system.service.impl;

import com.bootdo.system.dao.DlCompanyAlbumDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.bootdo.system.dao.DlCompanyDao;
import com.bootdo.system.domain.DlCompanyDO;
import com.bootdo.system.service.DlCompanyService;



@Service
public class DlCompanyServiceImpl implements DlCompanyService {
	public static final String[] SCALE = {"0-20人", "20-99人", "100-499人", "1000-9999人", "10000人以上"};
	public static final String[] FUNDING = {"未融资", "天使轮", "A轮", "B轮", "C轮", "D轮及以上", "已上市", "不需要融资"};
	@Autowired
	private RedisTemplate redisTemplate;


	@Autowired
	private DlCompanyDao dlCompanyDao;
	@Autowired
	private DlCompanyAlbumDao dlCompanyAlbumDao;

	@Override
	public DlCompanyDO get(Integer companyId){
		DlCompanyDO dlCompanyDO = dlCompanyDao.get(companyId);
		return dlCompanyDO;
	}
	
	@Override
	public List<DlCompanyDO> list(Map<String, Object> map){
		List<DlCompanyDO> list = dlCompanyDao.list(map);
		for (DlCompanyDO dlCompanyDO : list) {
			if(dlCompanyDO.getFunding() != null){
				dlCompanyDO.setFunding(FUNDING[Integer.valueOf(dlCompanyDO.getFunding())]);
			}
			if(dlCompanyDO.getScale() != null){
				dlCompanyDO.setScale(SCALE[Integer.valueOf(dlCompanyDO.getScale())]);
			}
		}

		return list;
	}
	
	@Override
	public int count(Map<String, Object> map){
		return dlCompanyDao.count(map);
	}
	
	@Override
	public int save(DlCompanyDO dlCompany){
		return dlCompanyDao.save(dlCompany);
	}
	
	@Override
	public int update(DlCompanyDO dlCompany, String[] ids){
		if(dlCompany.getLogo() != null){
			String[] split = dlCompany.getLogo().split("/");
			dlCompany.setLogo(split[split.length-1]);
		}
		if(dlCompany.getIdentityCardContrary() != null){
			String[] split = dlCompany.getIdentityCardContrary().split("/");
			dlCompany.setIdentityCardContrary(split[split.length-1]);
		}
		if(dlCompany.getIdentityCardFront() != null){
			String[] split = dlCompany.getIdentityCardFront().split("/");
			dlCompany.setIdentityCardFront(split[split.length-1]);
		}
		if(dlCompany.getBusinessLicense() != null){
			String[] split = dlCompany.getBusinessLicense().split("/");
			dlCompany.setBusinessLicense(split[split.length-1]);
		}
		if(dlCompany.getIdentityCardHand() != null){
			String[] split = dlCompany.getIdentityCardHand().split("/");
			dlCompany.setIdentityCardHand(split[split.length-1]);
		}
		if(ids != null && ids.length != 0){
			dlCompanyAlbumDao.updateStatus(ids, dlCompany.getAuditStatus());
		}
		return dlCompanyDao.update(dlCompany);
	}
	
	@Override
	public int remove(Integer companyId){
		return dlCompanyDao.remove(companyId);
	}
	
	@Override
	public int batchRemove(Integer[] companyIds){
		return dlCompanyDao.batchRemove(companyIds);
	}

	@Override
	public void auditFailureText(String auditFailureText, String id) {
		redisTemplate.opsForValue().set(id + "companyAuditFailureText" , auditFailureText);
	}

}
